package cn.hust.springboot.controller;

import cn.hust.springboot.bean.Emp;
import cn.hust.springboot.bean.Position;
import cn.hust.springboot.config.PaginationConstant;
import cn.hust.springboot.service.IEmpService;
import cn.hust.springboot.service.IPositionService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class PositionController {
    @Autowired
    IPositionService positionService;

    @Autowired
    IEmpService empService;


    // 查询所有
    @GetMapping("/positions")
    public String list(Model model,@RequestParam(value="pageNum",defaultValue="1")Integer pageNum) {

        if(ObjectUtils.isEmpty(pageNum)){
            pageNum= PaginationConstant.CURRENT_NUM;
        }
        //设置分页(当前页，和每页显示数据条数)
        PageHelper.startPage(pageNum, PaginationConstant.PAGE_SIZE);

        //查找数据
        List<Position> positions = positionService.getAllPositions();

        PageInfo<Position> pageInfo=new PageInfo<>(positions);

        //用Model传递对象到page页面
        model.addAttribute("pageInfo",pageInfo);
        return "position/position";
    }

    // 来到添加页面
    @GetMapping("/position")
    public String toAddPage(Model model) {
        // 来到添加页面
        List<Emp> emps = empService.getAllEmps();
        model.addAttribute("emps",emps);

        List<Position> positions = positionService.getAllPositions();
        model.addAttribute("positions",positions);

        return "position/add";
    }

    // 添加
    @PostMapping("/position")
    public String addPosition(Position position) {
        // 保存员工
        positionService.addPosition(position);
        // 来到列表页面
        // redirect: 表示重定向到一个地址 /代表当前项目路径
        // forward: 表示转发到一个地址
        return "redirect:/positions";
    }

    // 来到修改页面
    @GetMapping("/position/{id}")
    public String toUpdatePage(@PathVariable("id") Integer id, Model model) {
        Position position = positionService.getPositionById(id);
        model.addAttribute("position",position);

        List<Emp> emps = empService.getAllEmps();
        model.addAttribute("emps",emps);

        List<Position> positions = positionService.getAllPositions();
        model.addAttribute("positions",positions);

        // 回到修改页面（add是一个修改添加二合一的页面）
        return "position/add";
    }

    // 修改，需要提交id
    @PutMapping("/position")
    public String updatePosition(Position position) {
        positionService.updatePosition(position);
        return "redirect:/positions";
    }

    //删除
    @DeleteMapping("/position/{id}")
    public String deletePositionById(@PathVariable("id") Integer id){
        positionService.deletePositionById(id);
        return "redirect:/positions";
    }
//
//    @GetMapping("/positionaccount/{id}")
//    public String showPositionAccount(@PathVariable("id") Integer id, Model model) {
//        Position position = positionService.getPositionById(id);
//        model.addAttribute("position",position);
//
//        return "position/positionaccount";
//    }
//
    @PostMapping("/possearch")//提交表单+传回前端的映射
    public String queryPosition(@RequestParam String name, Model model,@RequestParam(value="pageNum",defaultValue="1")Integer pageNum){
        PageHelper.startPage(pageNum, PaginationConstant.PAGE_SIZE);
        //查找数据
        List<Position> positions = positionService.query(name);

        PageInfo<Position> pageInfo=new PageInfo<>(positions);

        model.addAttribute("pageInfo",pageInfo);
        return "position/position";
    }
}
